/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NodesCanvasPanel.java
 *
 * Created on 9 Ιουλ 2009, 12:12:50 μμ
 */

package gui;

import utilities.DrawingBean;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

/**
 * The canvas in which user defines the terrain details of nodes' positions
 * @author Arvanitis Ioannis
 */
public class NodesCanvasPanel extends javax.swing.JPanel{
    private static int cell_width, cell_height;
    private static int w, h;
    private static int obst_cell_w, obst_cell_h;
    private static Vector<DrawingBean> obstacles_v = new Vector<DrawingBean>(0);
    private static Vector<DrawingBean> nodes_v = new Vector<DrawingBean>(0);
    private static Color color;

    /**
     * Sets the width of canvas in meters
     * @param width Width of canvas in meters
     */
    public static void setW(int w) {
        NodesCanvasPanel.w = w;
    }

    /**
     * Gets the width of canvas in meters
     * @return the width of canvas in meters
     */
    public static int getW() {
        return w;
    }

    /**
     * Sets the height of canvas in meters
     * @param h Height of canvas in meters
     */
    public static void setH(int h) {
        NodesCanvasPanel.h = h;
    }

    /**
     * Gets the height of canvas in meters
     * @return the height of canvas in meters
     */
    public static int getH() {
        return h;
    }

    /**
     * Gets the width of each cell in canvas
     * @return the width of each cell in canvas
     */
    public static int getCell_width() {
        return cell_width;
    }

    /**
     * Gets the height of each cell in canvas
     * @return the height of each cell in canvas
     */
    public static int getCell_height() {
        return cell_height;
    }

    /**
     * Gets the height of each cell in canvas for obstacles
     * @return the height of each cell in canvas for obstacles
     */
    public static int getObst_cell_h() {
        return obst_cell_h;
    }

    /**
     * Sets the height of each cell in canvas for obstacles
     * @param cell_height The height of each cell in canvas for obstacles
     */
    public static void setObst_cell_h(int obst_cell_h) {
        NodesCanvasPanel.obst_cell_h = obst_cell_h;
    }

    /**
     * Gets the width of each cell in canvas for obstacles
     * @return the width of each cell in canvas for obstacles
     */
    public static int getObst_cell_w() {
        return obst_cell_w;
    }

    /**
     * Sets the width of each cell in canvas for obstacles
     * @param cell_width The width of each cell in canvas for obstacles
     */
    public static void setObst_cell_w(int obst_cell_w) {
        NodesCanvasPanel.obst_cell_w = obst_cell_w;
    }

    /**
     * Gets the vector with the mobility details
     * @return the vector with the mobility details
     */
    public static Vector<DrawingBean> getObstacles_v() {
        return obstacles_v;
    }

    /**
     * Sets the vector with the mobility details
     * @param v Vector with the mobility details
     */
    public static void setObstacles_v(Vector<DrawingBean> obstacles_v) {
        NodesCanvasPanel.obstacles_v = obstacles_v;
    }

    /**
     * Gets the vector with the nodes' position details
     * @return the vector with the nodes' position details
     */
    public static Vector<DrawingBean> getNodes_v() {
        return nodes_v;
    }

    /**
     * Sets the vector with the nodes' position details
     * @param v Vector with the nodes' position details
     */
    public static void setNodes_v(Vector<DrawingBean> nodes_v) {
        NodesCanvasPanel.nodes_v = nodes_v;
    }

    /**
     * Gets the current color in canvas
     * @return the current color in canvas
     */
    public static Color getColor() {
        return color;
    }

    /**
     * Sets the current color in canvas
     * @param color Current color in canvas
     */
    public static void setColor(Color color) {
        NodesCanvasPanel.color = color;
    }



    /**
     * Creates new form NodesCanvasPanel
     * @param w Width (in meters) of canvas
     * @param h Height (in meters) of canvas
     */
    public NodesCanvasPanel(int w, int h) {
        setW(w);
        setH(h);
        setSize(700,700*h/w);
        setBackground(Color.WHITE);
    }

    /**
     * Creates new form NodesCanvasPanel
     * @param w Width (in meters) of canvas
     * @param h Height (in meters) of canvas
     * @param c_obst_w Width (in cells) of canvas for obstacles
     * @param c_obst_h Height (in cells) of canvas for obstacles
     * @param v Vector containing the obstacles
     */
    public NodesCanvasPanel(int w, int h, int c_obst_w, int c_obst_h, Vector<DrawingBean> v) {
        setW(w);
        setH(h);
        setObst_cell_w(c_obst_w);
        setObst_cell_h(c_obst_h);
        setObstacles_v(v);
        setSize(700,700*h/w);
        setBackground(Color.WHITE);
    }

   
    @Override
    @SuppressWarnings("static-access")
    public void paintComponent(Graphics g) {
		super.paintComponent(g);

        if (obstacles_v.size() > 0) {
            cell_width = getWidth() / obst_cell_w;
            cell_height = getHeight() / obst_cell_h;
        }
        
        //paint the nodes
        for (int i=0; i<nodes_v.size(); i++) {
            g.setColor(nodes_v.get(i).getColor());
            int x, y, size;
            if (obstacles_v.size() == 0) {
                x = Math.round(((float)nodes_v.get(i).getPoint().x)/getW()*getWidth());
                y = Math.round(((float)nodes_v.get(i).getPoint().y)/getH()*getHeight());
                size = (getWidth() > getHeight())?getHeight()/50:getWidth()/50;
            }
            else {
                x = Math.round(((float)nodes_v.get(i).getPoint().x)/getW()*obst_cell_w*cell_width);
                y = Math.round(((float)nodes_v.get(i).getPoint().y)/getH()*obst_cell_h*cell_height);
                size = (obst_cell_w*cell_width > obst_cell_h*cell_height)?obst_cell_h*cell_height/50:obst_cell_w*cell_width/50;
            }
            g.fillOval(x-size/2, y-size/2, size, size);
            g.setColor(Color.BLUE);
            g.drawString(nodes_v.get(i).getInfo(), x, y);
        }

        //paint the obstacles if any
        if (obstacles_v.size() > 0) {
            for (int i=0; i<obstacles_v.size(); i++) {
                g.setColor(obstacles_v.get(i).getColor());
                g.fillRect(obstacles_v.get(i).getPoint().x*cell_width, obstacles_v.get(i).getPoint().y*cell_height, cell_width, cell_height);
            }
            //paint the borders
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(obst_cell_w*cell_width, 0, obst_cell_w*cell_width, getHeight());
            g.fillRect(0, obst_cell_h*cell_height, getWidth(), obst_cell_h*cell_height);
        }

    }

    /**
     * Finds the current position in a grid om meters
     * @param p Given point indicating the distance from the upper left corner
     * @return the current position in a grid om meters
     */
    public Point findCurrentPosition(Point p) {
        if (getObstacles_v().size() > 0) {
            return (new Point(w*p.x/(obst_cell_w*getCell_width()), h*p.y/(obst_cell_h*getCell_height())));
        }
        else {
            return (new Point(w*p.x/getWidth() , h*p.y/getHeight()));
        }
    }




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents



    








    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
